//
//  SEGHelenFortNavView.m
//  SEGHelenFort
//
//  Created by 梁学彰 on 2019/8/29.
//  Copyright © 2019 山刘. All rights reserved.
//

#import "SEGHelenFortNavView.h"
static CGFloat const kNav_H = 44.f;

@interface SEGHelenFortNavView ()
@property (nonatomic, strong) UIImageView *redView;
@property (nonatomic, strong) UILabel *communityNameLabel;

@end

@implementation SEGHelenFortNavView
#pragma mark - life cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initSubView];
        [self initNotification];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - init
- (void)initSubView {
    self.backgroundColor = [UIColor whiteColor];
    // 左边按钮
    CGRect leftBtnRect = CGRectMake(0, kStatusbarHeight, kScreenWidth/3*2, kNav_H);
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = leftBtnRect;
        [leftBtn addTarget:self
                    action:@selector(navLeftBtnClick)
          forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftBtn];
    // leftImage
    UIImage *leftImg = [UIImage imageNamed:SEGPicName(@"icon_home_location")];
    CGRect leftImgRect = CGRectMake(LYDistanceV(15), (leftBtn.height-leftImg.size.height)/2, leftImg.size.width, leftImg.size.height);
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:leftImgRect];
    leftImageView.image = leftImg;
    [leftBtn addSubview:leftImageView];
    // title
    UILabel *leftTitleLab = [[UILabel alloc] initWithFrame:CGRectZero];
    leftTitleLab.font = [UIFont boldSystemFontOfSize:18];
    leftTitleLab.textColor = kColor_1;
    leftTitleLab.text = kAccountCommunityName;
    [leftBtn addSubview:leftTitleLab];
    self.communityNameLabel = leftTitleLab;
    [self.communityNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftImageView.mas_right).offset(8);
        make.height.equalTo(leftBtn);
        make.top.equalTo(self).offset(kStatusbarHeight);
    }];
    
    [self refreshCommunityName];
    // 右边按钮
    CGRect rightBtnRect = CGRectMake(kScreenWidth-45 , kStatusbarHeight, 45, kNav_H);
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = rightBtnRect;
    [rightBtn addTarget:self
                 action:@selector(navRightBtnClick)
       forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightBtn];
    // rightImage
    UIImage *rightImg = [UIImage imageNamed:SEGPicName(@"icon_home_message")];
    CGRect rightImgRect = CGRectMake(7, (rightBtn.height-rightImg.size.height)/2, rightImg.size.width, rightImg.size.height);
    UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:rightImgRect];
    rightImageView.image = rightImg;
    [rightBtn addSubview:rightImageView];
    
    self.redView=[UIImageView new];
    UIImage*image=[UIImage imageNamed:SEGPicName(@"tab_red_round")];
    self.redView.bounds=CGRectMake(0, 0, image.size.width, image.size.height);
    self.redView.image=image;
    
    NSString *key = [NSString stringWithFormat:@"%@_%@_%@",SEGMessageRedDotUserDefaults, kAccountUserID, kAccountCommunityID];
    self.redView.hidden = ![[[NSUserDefaults standardUserDefaults] objectForKey:key] boolValue];
    self.redView.center=CGPointMake(rightImageView.centerX+rightImg.size.width*0.5,rightImageView.centerY-rightImg.size.height*0.5);
    [rightBtn addSubview:self.redView];
}

- (void)initNotification {
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshRed:) name:SEGHelenFortMainControllerMessageNotification object:nil];
}

#pragma mark - send request

#pragma mark - custom delegate

#pragma mark - system delegate

#pragma mark - event response
//刷新社区名字
- (void)refreshCommunityName {
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ ",kAccountCommunityName]];
    NSTextAttachment* locationArrow = [[NSTextAttachment alloc]init];
    
    UIImage *image =  [UIImage imageNamed:SEGPicName(@"icon_home_down")];
    locationArrow.image = image;
    locationArrow.bounds = CGRectMake(0, -2, image.size.width, image.size.height);
    [str appendAttributedString:[NSAttributedString attributedStringWithAttachment:locationArrow]];
    
    self.communityNameLabel.attributedText = str;
}
//切换社区
- (void)navLeftBtnClick {
    UIViewController* currentVC =[self currentViewController];
    UIViewController *VC = [[SEGUhomeMediator sharedInstance]seg_initChangeCommunityViewController:nil];
    if (currentVC.navigationController) {
        [currentVC.navigationController pushViewController:VC animated:YES];
    }
}
//点击消息
- (void)navRightBtnClick {
    self.redView.hidden = YES;
    //删除红点
    NSString *key = [NSString stringWithFormat:@"%@_%@_%@",SEGMessageRedDotUserDefaults, kAccountUserID, kAccountCommunityID];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
 
    [[SEGUhomeMediator sharedInstance] seg_selctedToTabWithAction:@"TAB_MESSAGE"];
}

// 实时刷新红点的状态
-(void)refreshRed:(NSNotification*)notice{
    NSNumber *num=notice.userInfo[@"RedPoint"];
    NSString *key = [NSString stringWithFormat:@"%@_%@_%@",SEGMessageRedDotUserDefaults, kAccountUserID, kAccountCommunityID];
    if (notice.userInfo&&num.integerValue==1) {
        self.redView.hidden = NO;
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:key];
    }else{
        self.redView.hidden =YES;
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:key];
    }
}

#pragma mark - private methods
- (UIViewController*)currentViewController{
    UIViewController* vc = [UIApplication sharedApplication].delegate.window.rootViewController;
    while (1) {
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController*)vc).selectedViewController;
        }
        
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController*)vc).visibleViewController;
        }
        
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }else{
            break;
        }
    }
    return vc;
}
#pragma mark - setter and getter

@end
